package com.location.footprint.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewStub;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.location.footprint.R;
import com.location.footprint.adapter.FragmentStateAdapter;
import com.location.footprint.base.BaseAppAplication;
import com.location.footprint.bean.CommentsTypeBean;
import com.location.footprint.bean.DataProvider;
import com.location.footprint.bean.FootPrintBean;
import com.location.footprint.fragment.CommentsFragment;
import com.location.footprint.fragment.LikesFragment;
import com.location.footprint.toolbar.ToolBarActivity;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;


public class DynamicActivity extends ToolBarActivity {


    @BindView(R.id.tablayout)
    TabLayout tablayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.viewStub_text_img)
    ViewStub viewStubTextImg;
    @BindView(R.id.viewStub_visiting)
    ViewStub viewStubVisiting;
    private TextView tab_textview;

    @Override
    public void onCreateCustomToolBar(Toolbar toolbar) {
        super.onCreateCustomToolBar(toolbar);
        View v = getLayoutInflater().inflate(R.layout.toobar_layout, toolbar);
//        TextView text_title = (TextView) v.findViewById(R.id.title_tooblar);
//        text_right = (TextView) v.findViewById(R.id.text_right);
//        Drawable drawable = ContextCompat.getDrawable(HistoryActivity.this, R.mipmap.ic_delete);
//        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//        text_right.setCompoundDrawables(null, null, drawable, null);
//        text_right.setText("");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dynamic);
        ButterKnife.bind(this);
        showHeader();
        initView();
    }

    private void initView() {
        FragmentStateAdapter adapter = new FragmentStateAdapter(getSupportFragmentManager());
        adapter.addFragment(LikesFragment.getInstance("赞"), "赞 121");
        adapter.addFragment(CommentsFragment.getInstance("评论"), "评论 6 ▾");
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(1);
        viewPager.setOffscreenPageLimit(adapter.getCount());
        tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                onTabSelect();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.setCustomView(null);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        tablayout.setupWithViewPager(viewPager);
    }

    private void showHeader() {
        int type = getIntent().getIntExtra(DataProvider.ITEM_TYPE, -1);
        switch (type) {
            case FootPrintBean.TEXT_IMG:
                if (viewStubTextImg != null) {
                    View stubView = viewStubTextImg.inflate();
                    LinearLayout footer = (LinearLayout) stubView.findViewById(R.id.layout_comments_footer);
                    footer.setVisibility(View.GONE);
                }
                break;
            case FootPrintBean.VISITING:
                if (viewStubVisiting != null) {
                    View stubView = viewStubVisiting.inflate();
                    LinearLayout footer = (LinearLayout) stubView.findViewById(R.id.layout_comments_footer);
                    footer.setVisibility(View.GONE);
                }
                break;
            default:
                break;
        }

    }

    public static void lanuch(Context context, int type) {
        Intent mIntent = new Intent(context, DynamicActivity.class);
        mIntent.putExtra(DataProvider.ITEM_TYPE, type);
        context.startActivity(mIntent);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.text_comments:
                EventBus.getDefault().post(new CommentsTypeBean(CommentsTypeBean.COMMENTS_SUBMIT));
                break;
            default:
                break;
        }
    }

    private void onTabSelect() {
        TabLayout.Tab tab = tablayout.getTabAt(tablayout.getSelectedTabPosition());
        if (tab.getCustomView() == null) {
            tab_textview = new TextView(this);
        }
        tab_textview.setGravity(Gravity.CENTER);
        tab_textview.setTextSize(16);
        tab_textview.setTextColor(ContextCompat.getColor(this, BaseAppAplication.theme));
        tab_textview.setText(tab.getText());
        tab.setCustomView(tab_textview);
        tablayout.setSelectedTabIndicatorColor(ContextCompat.getColor(this, BaseAppAplication.theme));
    }
}